package com.tanhua.server.controller;

import com.tanhua.commons.exception.TanHuaException;
import com.tanhua.model.vo.*;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.SettingService;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.Userservice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserControllers {


    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private SettingService settingService;

    @Autowired
    private Userservice userservice;


    /**
     * 查询用户信息
     *
     * @param userID
     * @param huanxinID
     * @return
     */
    @GetMapping()
    public ResponseEntity getUsers(@RequestParam(required = false, name = "userID") Long userID, @RequestParam(required = false) Long huanxinID) {
        if (huanxinID == null && userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo vo = userInfoService.getuserinfo(userID, huanxinID);
        return ResponseEntity.ok(vo);
    }

    /**
     * 更新用户信息
     *
     * @return
     */
    @PutMapping()
    public ResponseEntity updateUser(@RequestBody UserInfoVo userInfoVo) {
        userInfoService.updateUser(userInfoVo);
        return ResponseEntity.ok(null);
    }

    /**
     * 更新用户头像
     *
     * @return
     */
    @PostMapping("/header")
    public ResponseEntity updateHeader(MultipartFile headPhoto) {
        if (headPhoto == null) {
            throw new TanHuaException("请选择图片");
        }
        userInfoService.updateHeader(headPhoto);
        return ResponseEntity.ok(null);
    }

    /**
     * 通用设置
     *
     * @return
     */
    @GetMapping("/settings")
    public ResponseEntity userSetting() {
        SettingsVo settingsVo = userInfoService.getsettings();

        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 设置问题
     *
     * @param questionVo
     * @return
     */
    @PostMapping("/questions")
    public ResponseEntity setQuestions(@RequestBody QuestionVo questionVo) {

        settingService.setQuestion(questionVo);
        return ResponseEntity.ok(null);
    }

    /**
     * 通知设置
     *
     * @param settingsVo
     * @return
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody SettingsVo settingsVo) {

        settingsVo.setId(UserHolder.getUserId());

        settingService.notifications(settingsVo);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页获取黑名单
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/blacklist")
    public ResponseEntity Blacklist(@RequestParam Long page, @RequestParam Long pagesize) {
        // 限制pagesize的最大值，防止查100W条记录
        pagesize = pagesize > 50 ? 50 : pagesize;
        PageResult<UserInfoVoAge> pageResult = settingService.blackListPage(page, pagesize);
        return ResponseEntity.ok(pageResult);

    }

    /**
     * 移除黑名单
     *
     * @param uid
     * @return
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlack(@PathVariable Long uid) {
        userInfoService.deleteById(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号，发送验证码
     *
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity updatePhoneNumber() {
        Map<String, String> map = new HashMap<>();
        map.put("phone", UserHolder.getUser().getMobile());
        userservice.sendvalidate(map);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号，验证码校验
     *
     * @param verificationCode
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String, String> verificationCode) {
        boolean b = userservice.checkVerificationCode(verificationCode);
        Map<String, Object> map = new HashMap<>();
        map.put("verification", b);
        return ResponseEntity.ok(map);
    }

    /**
     * 修改手机号
     *
     * @param phone
     * @param token
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity newPhone(@RequestBody Map<String, String> phone, @RequestHeader("Authorization") String token) {
        userservice.updatePhone(phone, token);
        return ResponseEntity.ok(null);
    }

    /**
     * 缘分值
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}/fateValue")
    public ResponseEntity fateValue(@PathVariable Long id) {

        return ResponseEntity.ok(null);
    }

    /**
     * 是否喜欢
     *
     * @param uid
     * @return
     */
    @GetMapping("/{uid}/alreadyLove")
    public ResponseEntity alreadyLove(@PathVariable Long uid) {
        Boolean isLike = userservice.alreadyLove(uid);
        return ResponseEntity.ok(isLike);
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity Friends(Long page, @RequestParam(required = false) Long pagesize, @PathVariable String type) {
        switch (type) {
            case "1":       // 获取互相喜欢列表
                PageResult result = userservice.getFriends(page, pagesize);
                return ResponseEntity.ok(result);
            case "2":      // 获取喜欢列表
                PageResult likeResult = userservice.getLikeUser(page, pagesize);
                return ResponseEntity.ok(likeResult);
            case "3":      // 获取粉丝列表
                PageResult friendsResult = userservice.getFriend(page, pagesize);
                return ResponseEntity.ok(friendsResult);
            case "4":      // 获取谁看过我列表
                PageResult vo = userservice.visitors();
                return ResponseEntity.ok(vo);
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 喜欢用户
     *
     * @param uid
     * @return
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity likeUser(@PathVariable Long uid) {
        userservice.addFriend(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消喜欢
     *
     * @param uid
     * @return
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity unLike(@PathVariable Long uid) {
        userservice.unLike(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 获取互相喜欢，喜欢，粉丝数
     *
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity getCounts() {
        CountsVo vo = userservice.getCounts();
        return ResponseEntity.ok(vo);
    }

}
